<template>
  <el-container class="home-container">
    <el-header class="home-header">
      <div class="logo-box">
        <img src="../assets/images/heima.png" class="home-logo" alt />
        <h3>电商后台管理系统</h3>
      </div>
      <el-button type="info" @click="onExit">退出</el-button>
    </el-header>
    <el-container class="home-body">
      <el-aside class="home-aside" :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="onToggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          class="el-menu-vertical-demo"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu :key="item.id" v-for="(item) in menus" :index="item.id+''">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item
              :index="'/' + result.path"
              :key="result.id"
              v-for="(result) in item.children"
              @click="saveNavStatu('/' + result.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{result.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main class="home-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menus: null,
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created() {
    console.log(this.iconObj)
    this.getMenus()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    async getMenus() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menus = res.data
    },
    onExit() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    onToggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存当前路径
    saveNavStatu(activePath) {
      this.activePath = activePath
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
  .home-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    .logo-box {
      display: flex;
      align-items: center;
      .home-logo {
        display: block;
        border-radius: 50%;
        margin-right: 10px;
      }
      h3 {
        color: #fff;
        font-weight: 400;
      }
    }
  }
  .toggle-button {
    height: 24px;
    text-align: center;
    line-height: 24px;
    font-weight: 700;
    background-color: #4a5064;
    color: #fff;
    font-size: 10px;
    cursor: pointer;
    letter-spacing: 0.2em;
  }

  .el-menu {
    border-right: none;
  }
  .iconfont {
    padding-right: 15px;
  }
  .home-aside {
    background-color: #333744;
    height: 100%;
  }
}
</style>
